﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using ASBCC.Workflow.K2Manager;
using ASBCC.Workflow.Client;
using System.Data;

namespace BEGDC.CC.Web.Workflow.Demo
{
    public partial class EZVisitDemo : WorkflowBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EZVisitApplicationTableEntity entity = GetAppFormEntity();
                if (entity.AppID.HasValue)
                {
                    BindAppFormEntity(entity);
                }
                {
                    txtApplicantID.Text = UserInfo.EmployeeID.ToString();
                    txtApplicantCn.Text = UserInfo.NameChinese.ToString();
                    txtApplicantEn.Text = UserInfo.NameCN.ToString();
                    txtCreatorID.Text = UserInfo.EmployeeID.ToString();
                    txtCreatorCn.Text = UserInfo.NameChinese;
                    txtCreatorEn.Text = UserInfo.NameCN;
                }
            }
        }

        private void BindAppFormEntity(EZVisitApplicationTableEntity entity)
        {
            if (entity == null) return;
            txtAppFormID.Text = (entity.AppID ?? 0).ToString();
            txtAppFormNo.Text = entity.AppNo;
            txtApplicantID.Text = entity.RequesterID.ToString();
            txtApplicantEn.Text = entity.RequesterNameEN;
            txtApplicantCn.Text = entity.RequesterNameCN;
            txtCreateDate.Text = entity.CreateAt.HasValue ? entity.CreateAt.Value.ToString() : "";
            //txtCreatorID.Text = entity.CreatorID.ToString();
            //txtCreatorEn.Text = entity.CreatorEn;
            //txtCreatorCn.Text = entity.CreatorCn;
            txtFolio.Text = entity.Folio;
            txtModifiedDate.Text = entity.LatestUpdateAt.HasValue ? entity.LatestUpdateAt.Value.ToString() : "";
            txtProcInstID.Text = (entity.ProcInstId ?? 0).ToString();
            txtStatus.Text = entity.Status;
            txtProcessStatus.Text = entity.ProcessStatus;
        }

        private void SaveAppFormEntity()
        {
            EZVisitApplicationTableEntity entity = GetAppFormEntity();
            if (entity.AppID.HasValue)
            {
                entity.AppNo = txtAppFormNo.Text;
                entity.RequesterID = Convert.ToInt32(txtApplicantID.Text);
                entity.RequesterNameEN = txtApplicantEn.Text;
                entity.RequesterNameCN = txtApplicantCn.Text;

                DateTime createdDate;
                bool resCreateDate = DateTime.TryParse(txtCreateDate.Text, out createdDate);
                entity.CreateAt = resCreateDate ? createdDate : (DateTime?)null;

                //entity.CreatorID = Convert.ToInt32(txtCreatorID.Text);
                //entity.CreatorEn = txtCreatorEn.Text;
                //entity.CreatorCn = txtCreatorCn.Text;
                entity.Folio = txtFolio.Text;

                DateTime modifiedDate;
                bool resModifiedDate = DateTime.TryParse(txtModifiedDate.Text, out modifiedDate);
                entity.LatestUpdateAt = resModifiedDate ? modifiedDate : (DateTime?)null;

                int procInstId;
                bool res = int.TryParse(txtProcInstID.Text, out procInstId);
                entity.ProcInstId = res ? procInstId : (int?)null;

                EZVisitApplicationManager.Update(entity);
            }
            else
            {
                entity.AppNo = txtAppFormNo.Text;
                entity.RequesterID = Convert.ToInt32(txtApplicantID.Text);
                entity.RequesterNameEN = txtApplicantEn.Text;
                entity.RequesterNameCN = txtApplicantCn.Text;
                entity.CreateAt = DateTime.Now;
                //entity.CreatorID = Convert.ToInt32(txtCreatorID.Text);
                //entity.CreatorEn = txtCreatorEn.Text;
                //entity.CreatorCn = txtCreatorCn.Text;
                entity.LatestUpdateAt = DateTime.Now;

                int procInstId;
                bool res = int.TryParse(txtProcInstID.Text, out procInstId);
                entity.ProcInstId = res ? procInstId : (int?)null;

                ////////////////////////////////////////////////////////////////////////////
                // IMPORTANT: Insert Entity之前必须使用GetNewFolio()赋值给entity.Folio
                ////////////////////////////////////////////////////////////////////////////
                entity.Folio = GetNewFolio();

                EZVisitApplicationManager.Insert(entity);

                ///////////////////////////////////////////////////////////////////////////
                // IMPORTANT: Insert Entity之后必须保存AppFormId到ViewState的NewAppFormId中
                ///////////////////////////////////////////////////////////////////////////
                NewAppFormId = entity.AppID;
            }
        }



        #region Workflow Relational

        private EZVisitApplicationTableEntity _appFormEntity;

        private EZVisitApplicationTableEntity GetAppFormEntity()
        {
            if (_appFormEntity == null)
            {
                EZVisitApplicationTableEntity entity = null;
                int? appFormId = GetAppFormId();
                if (appFormId.HasValue)
                {
                    entity = EZVisitApplicationManager.SelectByKeys(appFormId.Value);
                    if (entity == null) throw new Exception(string.Format("Failed to Get EZVisit Application instance. ID={0}", appFormId));
                }
                else
                {
                    entity = new EZVisitApplicationTableEntity();
                }
                _appFormEntity = entity;
            }
            return _appFormEntity;
        }

        #endregion

        #region Workflow Virtual Members

        public override void RegisterWorkflowController()
        {
            _workflowController = new EZVisitWorkflowController(this, GetAppForm(), GetMyActivity());
        }

        public override PlaceHolder GetPlaceHolder()
        {
            return phWorkflow;
        }

        public override void BuildFormArguments()
        {
            base.BuildFormArguments();
            Dictionary<string, object> formArguments = GetFormArguments();
            // string TargetActivityName = GetCurrentTargetActivityName();

            formArguments[WorkflowKeyConsts.ModuleName] = "EZVisit Management";
            formArguments[EZVisitWorkflowKeyConsts.Applicant] = Convert.ToString(3637); //"di wan"3637
            formArguments[EZVisitWorkflowKeyConsts.CODPPManagerDone] = "1"; //"di wan"3637
            formArguments[EZVisitWorkflowKeyConsts.PDCDone] = "1"; //"di wan"3637
            formArguments[EZVisitActivityNameConsts.FNC_Controller] = GetDestination(EZVisitActivityNameConsts.FNC_Controller);
            formArguments[EZVisitActivityNameConsts.Dept_Head] = GetDestination(EZVisitActivityNameConsts.Dept_Head);
            formArguments[EZVisitActivityNameConsts.COD_PP_Check_Person] = GetDestination(EZVisitActivityNameConsts.COD_PP_Check_Person);
            formArguments[EZVisitActivityNameConsts.PDC] = GetDestination(EZVisitActivityNameConsts.PDC);
            formArguments[EZVisitActivityNameConsts.CEO_Delegation] = GetDestination(EZVisitActivityNameConsts.CEO_Delegation);
            formArguments[EZVisitActivityNameConsts.Post_Submission] = GetDestination(EZVisitActivityNameConsts.Post_Submission);
            formArguments[EZVisitActivityNameConsts.Post_COD_PP_Check_Person] = GetDestination(EZVisitActivityNameConsts.Post_COD_PP_Check_Person);
            formArguments[EZVisitActivityNameConsts.Post_Dept_Head] = GetDestination(EZVisitActivityNameConsts.Post_Dept_Head);
            formArguments[EZVisitActivityNameConsts.Post_Dept_Head] = GetDestination(EZVisitActivityNameConsts.Post_Dept_Head);

        }

        public override void InitActionPanel()
        {
            base.InitActionPanel();
        }

        public override void OnPreWorkflowLoadDone(EventArgs e)
        {
            base.OnPreWorkflowLoadDone(e);
        }

        public override void OnWorkflowLoadDone(EventArgs e)
        {
            base.OnWorkflowLoadDone(e);
        }

        #endregion

        #region IWorkflowPage Members

        public override string GetProcName()
        {
            return "EZVisit";
        }

        public override IAppForm GetAppForm()
        {
            return new EZVisitAppForm(GetAppFormEntity());
        }

        public override string GetRequestParamKey_AppFormId()
        {
            return base.GetRequestParamKey_AppFormId();
        }

        public override string GetRequestParamKey_MyActivityId()
        {
            return base.GetRequestParamKey_MyActivityId();
        }

        public override MyActivityTableEntity GetMyActivity()
        {
            return base.GetMyActivity();
        }

        public override IWorkflowActionPanel GetActionPanel()
        {
            return DefaultWorkflowActionPanel1;
        }

        public override IWorkflowControl GetProcessStampControl()
        {
            return base.GetProcessStampControl();
        }

        public override bool ExecuteAction(string actionType)
        {
            switch (actionType)
            {
                case K2ActionType.Save:
                    SaveAppFormEntity();
                    break;
                case K2ActionType.Submit:
                    SaveAppFormEntity();
                    break;
                default:
                    break;
            }
            return true;
        }

        public override void ExecuteActionDone(string actionType)
        {
            base.ExecuteActionDone(actionType);
        }

        private int[] GetDestination(string activityName)
        {
            DataTable dt = new DataTable();
            List<int> list = new List<int>();

            switch (activityName)
            {
                case EZVisitActivityNameConsts.Dept_Head:
                    list.Add(20334);
                    break;
                case EZVisitActivityNameConsts.COD_PP_Check_Person:
                    list.Add(27180);
                    break;
                case EZVisitActivityNameConsts.PDC:
                    list.Add(8471);
                    break;
                case EZVisitActivityNameConsts.CEO_Delegation:
                    list.Add(27647);
                    break;
                case EZVisitActivityNameConsts.Post_Submission:
                    list.Add(17409);
                    break;
                case EZVisitActivityNameConsts.Post_COD_PP_Check_Person:
                    list.Add(27180);
                    break;
                case EZVisitActivityNameConsts.Post_Dept_Head:
                    list.Add(20334);
                    break;
                default:
                    list.Add(1);
                    break;
            }
            return list.ToArray();
        }

        #endregion
    }
}
